package com.fh.shop.service.impl;/**
 * 描述:
 * Author:袁亚强
 * Date:2021/12/2017:56
 */

import com.alibaba.fastjson.JSONObject;
import com.fh.common.baseData.CommonData;
import com.fh.common.exceptions.CountException;
import com.fh.common.exceptions.ParamException;
import com.fh.shop.entity.po.CarData;
import com.fh.shop.entity.po.ShopBean;
import com.fh.shop.entity.po.ShopValues;
import com.fh.shop.mapper.ShopMapper;
import com.fh.shop.mapper.ShopValuesMapper;
import com.fh.shop.service.CarInfoService;
import com.fh.user.entity.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * created by macro on 2021/12/20,
 */
@Service
public class CarInfoServiceImpl implements CarInfoService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private ShopValuesMapper shopValuesMapper;

    @Override
    public void addCarInfo(Integer id, Integer sid, Integer count) {
        if (id == null || count == null) {
            throw new ParamException();
        }
        ShopBean shop = shopMapper.selectById(id);
        //验证库存
        Integer total = 0;//库存数
        Double price = 0D;//价格
        if (sid != null) {
            ShopValues shopValues = shopValuesMapper.selectById(sid);
            JSONObject parse = (JSONObject) JSONObject.parse(shopValues.getValuess());
            total = parse.getInteger("count");
            price = parse.getDouble("price");
        } else {
            total = shop.getRemarks();
            price = shop.getPrice();
        }
        if (total < count) {
            throw new CountException("库存只剩" + total);
        }

        CarData carData = new CarData();
        carData.setId(id);
        carData.setSid(sid);
        carData.setCount(count);
        carData.setPrice(price);
        carData.setImg(shop.getImg());
        carData.setTitle(shop.getIntro());
        User u = (User) request.getAttribute("u");
        List<CarData> carDataList = (List<CarData>) redisTemplate.opsForHash().get(CommonData.REDIS_KEY + CommonData.REDIS_PRO, u.getName());
        if (carDataList == null) {
            carDataList = new ArrayList<>();
            carDataList.add(carData);
        } else {
            boolean flag = false;
            for (CarData c : carDataList) {
                if (id.equals(c.getId())) {
                    if (sid == null) {
                        flag = true;
                        if (c.getCount() + count > total) {
                            throw new CountException("库存只剩" + total);
                        }
                        c.setCount(c.getCount() + count);
                        break;
                    } else {
                        if (sid.equals(c.getSid())) { //同规格商品
                            flag = true;
                            /*  验库存 */
                            if (c.getCount() + count > total) {
                                throw new CountException("库存只有" + total);
                            }
                            c.setCount(c.getCount() + count);
                            break;
                        }
                    }
                }
            }
            if (flag == false) {
                carDataList.add(carData);
            }
        }
        redisTemplate.opsForHash().put(CommonData.REDIS_KEY + CommonData.REDIS_PRO, u.getName(), carDataList);
    }

    @Override
    public void upCarInfo(String ids) {
        if (StringUtils.isEmpty(ids)) {
            User u = (User) request.getAttribute("u");
            List<CarData> carDataList = (List<CarData>) redisTemplate.opsForHash().get(CommonData.REDIS_KEY + CommonData.REDIS_PRO, u.getName());
            for (CarData carData : carDataList) {
                carData.setIsChecked(1);
            }

            redisTemplate.delete(CommonData.REDIS_KEY + CommonData.REDIS_PRO);
            redisTemplate.opsForHash().put(CommonData.REDIS_KEY + CommonData.REDIS_PRO, u.getName(), carDataList);
        }
        String substring = ids.substring(0, ids.length() - 1);
        System.out.println(substring);
        //  119:null,118:177,
        String[] split = substring.split(",");

        User u = (User) request.getAttribute("u");
        List<CarData> carDataList = (List<CarData>) redisTemplate.opsForHash().get(CommonData.REDIS_KEY + CommonData.REDIS_PRO, u.getName());
        String sb = new String();

        for (int i = 0; i < carDataList.size(); i++) {
            CarData c = carDataList.get(i);
            String ss= sb + c.getId() + ":" + c.getSid();
            System.out.println(ss);
            for (int j = 0; j < split.length; j++) {
                String a= split[j];
                if (split.length == 1) {
                    if (ss.equals(a) == true) {
                        c.setIsChecked(0);
                        break;
                    } else {
                        c.setIsChecked(1);
                        break;
                    }
                } else {
                    if (ss.equals(a) == true) {
                        c.setIsChecked(0);
                        break;
                    } else {
                        c.setIsChecked(1);
                    }

                }

            }
        }

        redisTemplate.delete(CommonData.REDIS_KEY + CommonData.REDIS_PRO);
        redisTemplate.opsForHash().put(CommonData.REDIS_KEY + CommonData.REDIS_PRO, u.getName(), carDataList);

     /*   //得到当前用户的购物车数据
        User u = (User) request.getAttribute("u");
        List<CarData> carDataList = (List<CarData>) redisTemplate.opsForHash().get(CommonData.REDIS_CARINFO_KEY+CommonData.REDIS_PRO, u.getName());
        //遍历当前用户购物车数据
        for (int i = 0; i <carDataList.size() ; i++) {
            CarData carData = carDataList.get(i);
            //判断当前商品是否要更新
            if(ids.contains(carData.getId()+":"+carData.getSid())){
                carData.setIsChecked(0);
            }else{
                carData.setIsChecked(1);
            }
        }
        //更加购物车的商品状态
        redisTemplate.opsForHash().put(CommonData.REDIS_CARINFO_KEY+CommonData.REDIS_PRO, u.getName(),carDataList);
*/
    }
}
